package com.wing.bill.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.model.response.MaterialCheckStatisticsVO;
import com.wing.bill.service.UnusualAndCheckNewReportService;
import com.wing.cabinet.model.response.StorageInventoryInoutVO;
import com.wing.cabinet.service.CheckedResultService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Validated
@Api(value = "Admin-刀具柜盘验记录报表", tags = "#/bill/checkNewReport")
@RestController("checkNewReportController")
@RequestMapping("bill/admin/checkNewReport")
@RequiredArgsConstructor
public class CheckNewReportController {

    /*private final CheckedResultService checkedResultService;

    @ApiOperation(value = "物料盘验统计（按天）顶部折线图")
    @GetMapping("/topLineChart")
    public JsonResult topLineChart(
        @RequestParam("param")String param  //qua 数量 money金额
        ,@RequestParam(defaultValue = "10") Integer days
    ){
        Map<String, Object> params = new HashMap<>();
        params.put("param", param);
        params.put("enterpriseCode", JwtUtils.getEnterpriseCode());
        params.put("begin", ReportUtil.days(days).get(0));
        params.put("end", ReportUtil.days(days).get(1));
        return checkedResultService.topLineChart(params, days);
    }

    @ApiOperation(value = "物料盘验数据统计")
    @GetMapping("/materialCheckStatistics")
    public JsonResult<MaterialCheckStatisticsVO> materialCheckStatistics(
        String mainCabCode
        , String timeType
        , String dayTime
        , String monthTime
        , String startTime
        , String endTime
    ){
        Map<String, Object> params = new HashMap<>();
        setParams(params, mainCabCode, timeType, dayTime, monthTime, startTime, endTime, null, null);
        return checkedResultService.materialCheckStatistics(params);
    }

    @ApiOperation(value = "物料盘验结果（按物料）柱状图")
    @GetMapping("/materialCheckResult")
    public JsonResult materialCheckResult(
        @RequestParam("param")String param  //qua 数量 money金额
        , Integer type  //1 按系列（SPU） 2 按编号（SKU）
        , String mainCabCode
        , String timeType
        , String dayTime
        , String monthTime
        , String startTime
        , String endTime
    ){
        Map<String, Object> params = new HashMap<>();
        setParams(params, mainCabCode, timeType, dayTime, monthTime, startTime, endTime, param, type);
        return checkedResultService.materialCheckResult(params);
    }

    @ApiOperation(value = "物料盘验排名（按柜子）饼状图")
    @GetMapping("/materialCheckRankingPieChart")
    public JsonResult materialCheckRankingPieChart(
        @RequestParam("param")String param  //qua 数量 money金额
        , Integer type  //1 按主柜 2 按副柜
        , String mainCabCode
        , String timeType
        , String dayTime
        , String monthTime
        , String startTime
        , String endTime
    ){
        Map<String, Object> params = new HashMap<>();
        setParams(params, mainCabCode, timeType, dayTime, monthTime, startTime, endTime, param, type);
        return checkedResultService.materialCheckRankingPieChart(params);
    }*/



    private void setParams(Map<String, Object> params, String mainCabCode, String timeType, String dayTime, String monthTime, String startTime, String endTime, String param, Integer type) {
        params.put("enterpriseCode", JwtUtils.getEnterpriseCode());
        if(StringUtils.isNotBlank(mainCabCode)){
            params.put("mainCabCode", mainCabCode);
        }
        if(StringUtils.isNotBlank(timeType)){
            if(timeType.equals("day")){ // 按日
                if(StringUtils.isNotBlank(dayTime)){
                    params.put("dayTime", dayTime);
                }
            }else if(timeType.equals("month")){ // 按月
                if(StringUtils.isNotBlank(monthTime)){
                    params.put("monthTime", monthTime);
                }
            }else if(timeType.equals("timeSlot")){ // 时间段
                if(StringUtils.isNotBlank(startTime)){
                    params.put("startTime", startTime);
                }
                if(StringUtils.isNotBlank(endTime)){
                    params.put("endTime", endTime);
                }
            }
        }
        if(StringUtils.isNotBlank(param)){
            params.put("param", param);
        }
        if(null != type){
            params.put("type", type);
        }
    }


    private final UnusualAndCheckNewReportService unusualAndCheckNewReportService;

    private final Integer abnormal = null;

    @ApiOperation(value = "顶部趋势图", notes = "报修统计(按天)")
    @GetMapping("/list")
    public JsonResult list(
            @RequestParam("param") String param //柱状图饼状图的下拉选 不同的选项 qua 数量 money 金额
    ){

        return unusualAndCheckNewReportService.list(abnormal,param, JwtUtils.getEnterpriseCode());
    }

    @ApiOperation(value = "顶部格子", notes = "搜索")
    @GetMapping("/topCell")
    public JsonResult topCell(
            @RequestParam("click")Integer click,
            @RequestParam("startTime")String startTime,
            @RequestParam("endTime")String endTime,
            @RequestParam("mainCabCode")String mainCabCode
    ){
        return unusualAndCheckNewReportService.topCell(abnormal,startTime,endTime,click,mainCabCode,JwtUtils.getEnterpriseCode());
    }

    @ApiOperation(value = "物料排名(按物料)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "selectParam", value="下拉选项参数(系列-spu 编号-sku)", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "param", value = "开关参数(数量-qua 金额-money)",  dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "tab", value = "tab参数(总览tab-tab0 物料tab-tab1)",  dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "businessType", value = "业务类型参数(0-缺失 1-暂存 2-报修 3-报废)",defaultValue = "null", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/midFirstChartNew")
    public JsonResult midFirstChartNew(
            @RequestParam("selectParam")String selectParam
            ,@RequestParam("param")String param
            ,@RequestParam("tab")String tab
            ,@RequestParam("startTime")String startTime
            ,@RequestParam("endTime")String endTime
            ,@RequestParam("mainCabCode") String mainCabCode
            , Integer businessType

    ){
        return unusualAndCheckNewReportService.queryStatisticsByCheck(businessType,selectParam,param,startTime,endTime,mainCabCode,JwtUtils.getEnterpriseCode(),"sku",tab);
    }

    @ApiOperation(value = "物料排名(按柜子)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "selectParam", value="下拉选项参数(主柜-main 副柜-desk)", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "param", value = "开关参数(数量-qua 金额-money)",  dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "tab", value = "tab参数(总览tab-tab0 柜子tab-tab1)",  dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "businessType", value = "业务类型参数(0-缺失 1-暂存 2-报修 3-报废)",defaultValue = "null", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/midSecondChartNew")
    public JsonResult midSecondChartNew(
            @RequestParam("selectParam")String selectParam
            ,@RequestParam("param")String param
            ,@RequestParam("tab")String tab
            ,@RequestParam("startTime")String startTime
            ,@RequestParam("endTime")String endTime
            ,@RequestParam("mainCabCode") String mainCabCode
            , Integer businessType
    ){
        return unusualAndCheckNewReportService.queryStatisticsByCheck(businessType,selectParam,param,startTime,endTime,mainCabCode,JwtUtils.getEnterpriseCode(),"cab",tab);
    }

    @ApiOperation(value = "物料排名(按组织)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "selectParam", value="下拉选项参数(存入人-person 部门-department 成本中心-costCenter)", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "param", value = "开关参数(数量-qua 金额-money)",  dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "tab", value = "tab参数(总览tab-tab0 组织tab-tab1)",  dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "businessType", value = "业务类型参数(0-缺失 1-暂存 2-报修 3-报废)",defaultValue = "null", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/midThirdChartNew")
    public JsonResult midThirdChartNew(
            @RequestParam("selectParam")String selectParam
            ,@RequestParam("param")String param
            ,@RequestParam("tab")String tab
            ,@RequestParam("startTime")String startTime
            ,@RequestParam("endTime")String endTime
            ,@RequestParam("mainCabCode") String mainCabCode
            , Integer businessType
    ){
        return unusualAndCheckNewReportService.queryStatisticsByCheck(businessType,selectParam,param,startTime,endTime,mainCabCode,JwtUtils.getEnterpriseCode(),"org",tab);
    }

    @ApiOperation(value = "物料排名(按使用情况)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "selectParam", value="下拉选项参数(工单-workOrder 车间-workShop 设备-device)", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "param", value = "开关参数(数量-qua 金额-money)",  dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "tab", value = "tab参数(总览tab-tab0 使用情况tab-tab1)",  dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "businessType", value = "业务类型参数(0-缺失 1-暂存 2-报修 3-报废)",defaultValue = "null", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/midFourChartNew")
    public JsonResult midFourChartNew(
            @RequestParam("selectParam")String selectParam
            ,@RequestParam("param")String param
            ,@RequestParam("tab")String tab
            ,@RequestParam("startTime")String startTime
            ,@RequestParam("endTime")String endTime
            ,@RequestParam("mainCabCode") String mainCabCode
            , Integer businessType
    ){
        return unusualAndCheckNewReportService.queryStatisticsByCheck(businessType,selectParam,param,startTime,endTime,mainCabCode,JwtUtils.getEnterpriseCode(),"use",tab);
    }

    @ApiOperation(value = "盘验结果统计", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessTypePre", value="盘验结果统计参数(暂存-1 报修-2 报废-3)", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/midFiveChartNew")
    public JsonResult midFiveChartNew(
            @RequestParam("businessTypePre")Integer businessTypePre
            ,@RequestParam("startTime")String startTime
            ,@RequestParam("endTime")String endTime
            ,@RequestParam("mainCabCode") String mainCabCode
            ,Long creator
    ){
        return unusualAndCheckNewReportService.queryCheckResultStatistics(businessTypePre,creator,startTime,endTime,mainCabCode,JwtUtils.getEnterpriseCode());
    }

    @ApiOperation(value = "盘验记录列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/dataList")
    public PageResult<StorageInventoryInoutVO> dataList(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam("startTime")String startTime
            ,@RequestParam("endTime")String endTime
            ,@RequestParam("mainCabCode") String mainCabCode
    ) {
        Page<StorageInventoryInoutVO> pg = new Page<>(page, limit);

        IPage<StorageInventoryInoutVO> list = unusualAndCheckNewReportService.data(startTime,endTime,mainCabCode,JwtUtils.getEnterpriseCode(),pg);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }
}
